package com.example.demo.controller;


import com.example.demo.entity.StudentInfo;
import com.example.demo.service.StudentInfoService;
import com.example.demo.vo.StudentClassInfo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@CrossOrigin
@RestController
@RequestMapping("stu")

public class StudentInfoController {
    @Autowired
    private StudentInfoService studentInfoService;
    //http://localhost:8080/stu/query
    @GetMapping("/query")
    public List<StudentInfo> queryStudentInfoByName(String name) {
        return studentInfoService.getAllStudentInfo(name);
    }
    //http://localhost:8080/stu/querystu
    @GetMapping("/querystu")
    public List<StudentClassInfo> queryStudentClassInfoByName(@RequestParam String name,@RequestParam Integer clsid) {
        List<StudentClassInfo> stuClass = null;
        //当clsid不为0表示根据班级查学生信息
        if(clsid==0) {//clsid = 0表示返回所有学生信息
           stuClass = studentInfoService.getAllStuAndClassByName(name);
        }else {
            stuClass = studentInfoService.getAllClassByclsAndName(name, clsid);
        }
        return stuClass;
    }
    //http://localhost:8080/stu/del/195
    @GetMapping("del/{id}")
    private Integer delStudentInfoById(@PathVariable Integer id) {
        return studentInfoService.deleteByID(id);
    }

    // http://localhost:8080/stu/add
    @PostMapping("/add")
    public Integer addStudentInfo(@RequestBody StudentInfo studentInfo) {
        return studentInfoService.insertStudentInfo(studentInfo);
    }
    // http://localhost:8080/stu/update
    @PostMapping("/update")
    public Integer updateStudentInfo(@RequestBody StudentInfo studentInfo) {
        return studentInfoService.updateStudentInfo(studentInfo);
    }

    @GetMapping("/page")
    public PageInfo<StudentClassInfo> queryStudentClassInfo(@RequestParam Integer page, @RequestParam Integer size) {
        return studentInfoService.getPageStudentInfoClassInfo(page,size);
    }
    /// having a test
}
